<?php if(!isset($_SESSION)){session_start();}; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php echo DESCRIPTION; ?>
<?php echo KEYWORD; ?>
<title><?php echo WEB_TITLE;?></title>
<link href="<?php echo  WEB_URL;?>/styles/style.css" rel="stylesheet" />
<link href="<?php echo  WEB_URL;?>/styles/ads.css" rel="stylesheet" />
<link type="text/css" href="<?php echo  WEB_URL;?>/styles/ui-lightness/jquery-ui-1.8rc2.custom.css" rel="stylesheet" />

<!-- StyleSheet Plugin-->
<link type="text/css" href="<?php echo  WEB_URL;?>/styles/jqtransform.css" rel="stylesheet" />

<script type="text/javascript" src="<?php echo  WEB_URL;?>/scripts/jquery/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="<?php echo  WEB_URL;?>/scripts/jquery/jquery-ui-1.8rc2.custom.min.js"></script>

<!-- Script Plugin -->
<script type="text/javascript" src="<?php echo  WEB_URL;?>/scripts/jquery/jquery.jqtransform.js"></script>
<script type="text/javascript" src="<?php echo  WEB_URL;?>/scripts/jquery/tab.js"></script>
<script type="text/javascript" src="<?php echo WEB_URL;?>/scripts/jquery/jquery.ajaxupload.3.5.js"></script>

<script type="text/javascript">
$(function(){
	var total;
	var price;
	var discount;
	var position = "";
	
	$('form').jqTransform({imgPath:'<?php echo  WEB_URL;?>/styles/images/jqtransform/'});
	
	$("#addbanner_form #posection div.jqTransformSelectWrapper ul li a").click(function(){
		var value= $(this,"#posection div.jqTransformSelectWrapper span").text();
		if(value == "big_1" || value == "big_2"){
			position = "big";
		}else{
			position = "";
		}
		$.ajax({
			type: "POST",
			url: "../query.php",
			data: {	get_pos_price:true, pos_name:value},
			dataType: 'json',
			success: function(msg){	
				if(msg.status == 1){
					price = msg.price;
					$('#pos_price').attr('value',msg.price);
					$('#pos_discount').val('0%');
					total = $('#pos_discount').val().replace('%','');
					total = (total != 0 ? (price - ((total*price)/100)) : price);
					$('#total').val(total);
				}else{
					alert('ไม่สามารถประมวลผลได้');
				}
			}			   
		});
		return false; //prevent default browser action
	});
	
	$("#addbanner_form #persection div.jqTransformSelectWrapper ul li a").click(function(){
		var per 	= $(this,"#persection div.jqTransformSelectWrapper span").text();
			per		= per.split(' ');
		if(position == "big"){
			discount 	= (per[0] >= 1 && per[0] < 3 ? 0 :(per[0] > 2 && per[0] < 6 ? 7:(per[0] > 5 && per[0] < 12 ? 15 : (per[0] > 11 ? 25 : 0 ))));
		}else{
			discount 	= (per[0] >= 1 && per[0] < 3 ? 0 :(per[0] > 2 && per[0] < 6 ? 5:(per[0] > 5 && per[0] < 12 ? 10 : 20 )));
		}
		price		= (typeof(price) != "undefined" ? price : $('#pos_price').val());
		total 		= per[0] * (discount != 0 ? (price - ((discount*price)/100)) : price);
		$('#total').val(total);	
		$('#pos_discount').val(discount+'%')
		return false; //prevent default browser action
	});
	
	var btnUpload=$('#upload');
	var status=$('#status');
	new AjaxUpload(btnUpload, {
		action: '../upload-banner.php',
		name: 'uploadfile',
		onSubmit: function(file, ext){			
			if (!(ext && /^(jpg|png|jpeg|gif)$/.test(ext))){ 
				// extension is not allowed 
				status.text('ไฟล์ที่คุณสามารถอัพโหลดได้คือ .jpg, .jpeg, .png และ .gif เท่านั้น');
				return false;
			}
			status.text('Uploading...');
		},
		onComplete: function(file, response){
			//On completion clear the status
			status.text('');
			//Add uploaded file to list
			var imgstatus = response.split(',');
			
			if(imgstatus[0]==="success"){
				$('#banner li').remove();
				$('<li></li>').appendTo('#banner').html('<img src="../uploads/'+imgstatus[1]+'" alt="'+imgstatus[1]+'" /><input type="hidden" name="img[]" value="'+imgstatus[1]+'"/>').addClass('success');
				count_img++;
				
			} else{
				$('<li></li>').appendTo('#banner').text(imgstatus[1]).addClass('error');
			}

		}
	});
	
	$('#post_banner').click(function(){
		var banner_count = $('#banner li').size();
		var uname 	= $('#uid').val();
		var cat		= $('#category').val();
		var pos_size= $('#position').val();
		var url		= $('#url').val();
		var pos		= pos_size.split(',')[0];
		var period	= $('#banner_period').val();
		var img		= $('#banner li img').attr('alt');
		var size	= $('#banner li img').width()+'x'+$('#banner li img').height();
		var size_on_pos = pos_size.split(',')[1]
		var price	= $('#pos_price').val();

		if(banner_count){
			if(size_on_pos == size){
				$.ajax({
					type: "POST",
					url: "../query.php",
					data: {	post_banner:true, 
							username:uname, 
							category:cat, 
							position:pos, 
							banner_period:period, 
							banner_img:img, 
							banner_size:size,
							url:url,
							pos_price:price,
							discount:discount,
							total:total },
					dataType: 'json',
					beforeSend: function(x){$('#loading').html('<img src="../styles/images/loadingx.gif"/>');},
					success: function(msg){	
						if(msg.status == 1){
							alert('ลงประกาศแบนเนอร์สำเร็จแล้วคะ');
							location.reload();
						}else{
							alert(msg.status);
							alert('ไม่สามารถลงประกาศได้');
						}
					}			   
				});
			}else{
				alert('ตำแหน่งที่คุณเลือกสามารถลงติดแบนเนอร์ขนาด '+size_on_pos+' เท่านั้นคะ');
			}
		}else{
			alert('กรุณาอัพโหลดแบนเนอร์ก่อนนะคะ');
		}
		return false;
	});
	
});
</script>

</head>

<body>
	<div class="page">
    
    	<?php require_once('../header.php');?>
        
        <div class="content">
        	<div id="wrap_top">
            	<div id="wrap_top_left">
                	<!--<div id="wrap_ads">                	 
                        <div id="wrap_728x90"></div>
                	</div>-->
   
                    <div class="tabs">
                          <a tab="#tab_my_profile">แบนเนอร์ของฉัน</a>              
                    </div>
            
                    <div id="tab_my_profile" class="tab_page" style="min-height:400px;">
                    	<div id="banner_section">
                        <form action="#" method="post" enctype="multipart/form-data" id="addbanner_form">
                        <table width="730" border="0" cellspacing="2" cellpadding="2">
                            <tr>
                              <td><div align="right"><strong>หมวดหมู่</strong></div></td>
                              <td colspan="3">
                              <select name="category" id="category">
                              	<option value="99">ทุกหมวดหมู่</option>		
                                <?php
                                      $qr = $db->select_query('SELECT * FROM category');
                                      while($getcat = $db->fetch($qr)){
                                          echo '<option value="'.$getcat['cat_id'].'">'.$getcat['cat_name'].'</option>';
                                      }
                                  ?>
                              </select>
                              </td>
                            </tr>
                            <tr>
                              <td><div align="right"><strong>ตำแหน่งที่ติดประกาศได้</strong></div></td>
                              <td width="186">
                              <div id="posection">
                              <select name="position" id="position">		
                                <?php
										/*
										SELECT
										  `positions`.`position_name`
										FROM
										  `category`, `positions`
										WHERE
										  `positions`.`position_name` NOT IN ("")
										GROUP BY
										  `positions`.`position_name`
										*/
									  $sql_p =' SELECT *
												FROM
												  `positions`
												WHERE
												  `positions`.`id` NOT IN (SELECT position_id FROM banners) AND 
  												  `positions`.`id` NOT BETWEEN 10 AND 19';
                                      $qp = $db->select_query($sql_p);
                                      while($getpos = $db->fetch($qp)){
                                          echo '<option value="'.$getpos['id'].','.$getpos['size'].'">'.$getpos['position_name'].'</option>';
                                      }
                                  ?>
                              </select>
                              </div>
                              </td>
                              <td width="38"><div align="right"><strong>ราคา</strong></div></td>
                              <td width="329">
                              	<?php
                                	$first_q = $db->select_query('SELECT 
																  `positions`.`price`
																FROM
																  `positions`
																WHERE
																  `positions`.`id` NOT IN (SELECT position_id FROM banners)
																ORDER BY id LIMIT 1');
									$first_price = $db->fetch($first_q);
								?>
                              	<input type="text" id="pos_price" disabled="disabled" value="<?php echo $first_price['price'];?>" />
                              </td>
                            </tr>
                            <tr>
                              <td><div align="right"><strong>ระยะเวลาลงประกาศ</strong></div></td>
                              <td>
                              <div id="persection">
                                <select name="banner_period" id="banner_period">
                                  <option value="1">1 เดือน</option>
                                  <option value="2">2 เดือน</option>
                                  <option value="3">3 เดือน</option>
                                  <option value="4">4 เดือน</option>
                                  <option value="5">5 เดือน</option>
                                  <option value="6">6 เดือน</option>
                                  <option value="7">7 เดือน</option>
                                  <option value="8">8 เดือน</option>
                                  <option value="9">9 เดือน</option>
                                  <option value="10">10 เดือน</option>
                                  <option value="11">11 เดือน</option>
                                  <option value="12">12 เดือน</option>
                                  </select>
                              </div>
                              </td>
                              <td><strong>ส่วนลด</strong></td>
                              <td><input type="text" id="pos_discount" disabled="disabled" value="0%"/></td>
                            </tr>
                            <tr>
                              <td><div align="right"><strong>ราคารวม(บาท)</strong></div></td>
                              <td colspan="3"><input type="text" name="total" id="total" value="<?php echo $first_price['price'];?>" size="40"/></td>
                            </tr>
                            <tr>
                              <td><div align="right"><strong>URL</strong></div></td>
                              <td colspan="3"><input type="text" name="url" id="url" value="http://" size="40"/></td>
                            </tr>
                            <tr>
                              <td width="151"><div align="right"><strong>Banner Image</strong></div></td>
                              <td colspan="3"> <div id="upload" ><span>อัพโหลดรูป</span></div><span id="status" ></span></td>
                            </tr>
                            <tr>
                              <td colspan="4">
                              	<ul id="banner" style="min-height:100px;">
                                <?php
									$pic_num = isset($_SESSION['banner_img'])? count($_SESSION['banner_img']):0;
									if($pic_num != 0){
										echo '<li><img src="../uploads/'.$_SESSION['banner_img'].'" alt="'.$_SESSION['banner_img'].'"/></li>';
									}
								?>
                                </ul>
                                <a class="large button blue" id="post_banner" href="#">ลงประกาศ!</a><span id="loading"></span>
                                <div style="font-weight:bold;color:red;margin:10px 0px;"><b style="color:red;">*</b>เมื่อลงประกาศแล้วกรุณาทำการโอนเงินแล้วแจ้งเจ้าหน้าที่ภายใน 1 อาทิตย์ เพราะเจ้าหน้าที่จะลบแบนเนอร์ที่ทำรายการไม่เรียบร้อย</div>
                                <div style="font-weight:bold;color:red;margin:10px 0px;"><b style="color:red;">*</b>เพื่อความรวดเร็วในการตรวจสอบกรุณาโอนมาเป็นเศษสตางค์ เช่น 300.10, 300.20, 300.30  เป็นต้น</div>
                                <input type="hidden" id="uid" value="<?php echo $_SESSION['usr'];?>"  />
                              </td>
                            </tr>  
                        </table>

                        </form>
                        </div>
                        <div class="clear" style="margin-top:10px;"></div>
                    <?php
                    	$sql = 'SELECT
								  `banners`.*, `positions`.`position_name`, `category`.`cat_name`
								FROM
								  `banners` INNER JOIN
								  `positions` ON `positions`.`id` = `banners`.`position_id` INNER JOIN
								  `category` ON `category`.`cat_id` = `banners`.`cat_id`
								WHERE
								  `banners`.`user_id` = "'.$_SESSION['usr'].'"';
						$q = $db->select_query($sql);
						$c = $db->rows($q);
					?>
                    	<table width="730" border="1" cellspacing="2" cellpadding="5" id="list">
                          <thead>
                          	<tr>
                          		<th width="100">Banner ID</th>
                                <th width="100">ตำแหน่ง</th>
                                <th width="100">หมวดหมู่</th>
                                <th width="50">ขนาด</th>
                                <th width="100">ระยะเวลาประกาศ</th>
                                <th width="100">เริ่ม</th>
                                <th width="100">สิ้นสุด</th>
                                <th width="50">สถานนะ</th>
                           	</tr>
                          </thead>
                          <tbody>
                    <?php
						if($c == "" || $c <= 0){
					?>
                    		<tr align="center">
                                <td colspan="8">
                                <div style="color: #0369C0; font-size: 18px; font-weight: bold; padding: 10px; text-align: center;">ยังไม่การติดแบนเนอร์ใดๆ</div>
                                </td>
                            </tr>
                    <?php	
						}
						while($rs = $db->fetch($q)){
					?>
                          	<tr align="center">
                                <td><?php echo $rs['banner_id'];?></td>
                                <td><?php echo $rs['position_name'];?></td>
                                <td><?php ($rs['cat_id'] == 99)? print 'ทุกหมวด' : $rs['cat_name'];?></td>
                                <td><?php echo $rs['size'];?></td>
                                <td><?php echo $rs['period'].' เดือน';?></td>
                                <td><?php echo ThaiTimeConvert($rs['create_at'],0,0);?></td>
                                <td><?php echo ThaiTimeConvert($rs['banner_expire'],0,0);?></td>
                                <td><?php ($rs['active']==0)? print '<b style="color:red;">รออนุมัติ</b>' : print '<b style="color:green;">ทำงาน</b>';?></td>
                          	</tr>                          
                            <tr>
                            	<td colspan="8" align="center"><a href="<?php echo $rs['url'];?>"><img src="../uploads/<?php echo $rs['image'];?>" /></a></td>
                            </tr>
                    <?php
						}
					?>
                    	</tbody>
                        </table>
                       
                    </div>
                    
                    <script type="text/javascript">
                    <!--
                    $.tabs('.tabs a'); 
                    //-->
                    </script>
                                    
                                                                
              	</div>
              	
              	<?php require('user_menu.php');?>
                
            </div>
            
            
        </div>
        
        <?php require_once('../footer.php');?>
        
    </div>
</body>
</html>